package smedi.ismedi.web.bff.controller.quality;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.instrument.api.dto.request.*;
import com.smedi.ismedi.instrument.api.dto.response.AddInstrumentConfigurationListResponse;
import com.smedi.ismedi.instrument.api.dto.response.InstrumentConfigurationResponse;
import com.smedi.ismedi.instrument.api.dto.response.InstrumentResponse;
import com.smedi.ismedi.instrument.api.feign.RemoteInstrumentFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import smedi.ismedi.web.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@Api(value = "计量器具管理接口", tags = {"计量器具管理接口"})
@RequestMapping(value = "/webbff/v1")
@RestController
@RequiredArgsConstructor
public class RemoteInstrumentController {
    private final RemoteInstrumentFeignApi remoteInstrumentFeignApi;

    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;

    /**
     * 通过文件导入添加 计量器具配置
     *
     * @param file 计量器具 新增 请求
     * @return ResponseData<AddInstrumentConfigurationListResultResponse>
     */
    @ApiOperation(value = "计量器具配置新增", nickname = "计量器具配置新增")
    @PostMapping(value = "/instrument-configuration/addList",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseData<List<AddInstrumentConfigurationListResponse>> importInstrumentConfiguration(@ApiParam(value = "file", required = true) @RequestPart(value = "file") MultipartFile file){
        return remoteInstrumentFeignApi.importInstrumentConfiguration(file);
    }

    /**
     * 分页查找 计量器具配置
     *
     * @param instrumentConfigurationPageRequest 计量器具配置 分页查找 请求
     * @return ResponseData<List<InstrumentConfigurationResponse>>
     */
    @ApiOperation(value = "计量器具配置分页查找", nickname = "计量器具配置分页查找")
    @PostMapping(value = "/instrument-configuration/page")
    ResponseData<PaginationData<InstrumentConfigurationResponse>> getInstrumentConfigurationPage(@Validated @RequestBody InstrumentConfigurationPageRequest instrumentConfigurationPageRequest){
        return remoteInstrumentFeignApi.getInstrumentConfigurationPage(instrumentConfigurationPageRequest);
    }


    /**
     * 新增 计量器具
     *
     * @param instrumentCreateRequest 计量器具 新增 请求
     * @return ResponseData<Boolean>
     */
    @ApiOperation(value = "计量器具新增", nickname = "计量器具新增")
    @PostMapping(value = "/instrument/create")
    ResponseData<InstrumentResponse> createInstrument(@Validated @RequestBody InstrumentCreateRequest instrumentCreateRequest){
        return remoteInstrumentFeignApi.createInstrument(instrumentCreateRequest);
    }

    /**
     * 编辑 计量器具
     *
     * @param instrumentEditRequest 计量器具 编辑 请求
     * @return ResponseData<Boolean>
     */
    @ApiOperation(value = "计量器具编辑", nickname = "计量器具编辑")
    @PostMapping(value = "/instrument/edit")
    ResponseData<InstrumentResponse> editInstrument(@Validated @RequestBody InstrumentEditRequest instrumentEditRequest){
        return remoteInstrumentFeignApi.editInstrument(instrumentEditRequest);
    }

    /**
     * 校准 计量器具
     *
     * @param instrumentVerifyRequest 计量器具 校准 请求
     * @return ResponseData<InstrumentResponse>
     */
    @ApiOperation(value = "计量器具校准", nickname = "计量器具校准")
    @PostMapping(value = "/instrument/verify")
    ResponseData<InstrumentResponse> verifyInstrument(@Validated @RequestBody InstrumentVerifyRequest instrumentVerifyRequest){
        return remoteInstrumentFeignApi.verifyInstrument(instrumentVerifyRequest);
    }

    /**
     * 删除 计量器具
     *
     * @param instrumentRemoveRequest 计量器具 删除 请求
     * @return ResponseData<Boolean>
     */
    @ApiOperation(httpMethod = "POST", value = "删除 计量器具")
    @PostMapping(value = "/instrument/remove")
    ResponseData<Boolean> removeInstrument(@Validated @RequestBody InstrumentRemoveRequest instrumentRemoveRequest){
        return remoteInstrumentFeignApi.removeInstrument(instrumentRemoveRequest);
    }

    /**
     * 获取 计量器具 单一
     *
     * @param instrumentOneRequest 计量器具 单一 请求
     * @return ResponseData<InstrumentResponse>
     */
    @ApiOperation(value = "获取 计量器具 单一", nickname = "获取 计量器具 单一")
    @PostMapping(value = "/instrument/one")
    ResponseData<InstrumentResponse> getInstrumentOne(@Validated @RequestBody InstrumentOneRequest instrumentOneRequest){
        return remoteInstrumentFeignApi.getInstrumentOne(instrumentOneRequest);
    }

    /**
     * 获取 计量器具 列表
     *
     * @param instrumentListRequest 计量器具 列表 请求
     * @return ResponseData<List < InstrumentResponse>>
     */
    @ApiOperation(value = "获取 计量器具 列表", nickname = "获取 计量器具 列表")
    @PostMapping(value = "/instrument/list")
    ResponseData<List<InstrumentResponse>> getInstrumentList(@Validated @RequestBody InstrumentListRequest instrumentListRequest){
        return remoteInstrumentFeignApi.getInstrumentList(instrumentListRequest);
    }

    /**
     * 获取 计量器具 分页
     *
     * @param instrumentPageRequest 计量器具 分页 请求
     * @return ResponseData<PaginationData <InstrumentResponse>>
     */
    @ApiOperation(value = "获取 计量器具 分页", nickname = "获取 计量器具 分页")
    @PostMapping(value = "/instrument/page")
    ResponseData<PaginationData<InstrumentResponse>> getInstrumentPage(@Validated @RequestBody InstrumentPageRequest instrumentPageRequest){
        return remoteInstrumentFeignApi.getInstrumentPage(instrumentPageRequest);
    }


    /**
     * 导出计量器具
     *
     * @param instrumentExportRequest 计量器具 导出 请求
     * @return ResponseData<Boolean>
     */
    @ApiOperation(httpMethod = "POST", value = "导出计量器具")
    @PostMapping(value = "/instrument/export")
    ResponseData<String> exportInstrument(@Validated @RequestBody InstrumentExportRequest instrumentExportRequest, HttpServletResponse response)  throws IOException{
        //String urlOut = remoteInstrumentFeignApi.exportInstrument(instrumentExportRequest).getData();
        return remoteInstrumentFeignApi.exportInstrument(instrumentExportRequest);
//        if(urlOut == null){
//            return new ResponseData<>().success("null");
//        }else {
//            FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
//            FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);
//            return new ResponseData<>().success(true);
//        }

    }

    /**
     * 临期/超期提醒弹窗(返回临期/超期的设备名称）前端每次在用户登录时调用
     */

    @ApiOperation(httpMethod = "POST", value = "临期/超期提醒弹窗")
    @PostMapping(value = "/instrument/overdueReminder")
    ResponseData<List<String>> overdueReminder(){
        return remoteInstrumentFeignApi.overdueReminder();
    }
}
